//
//  FeedbackController.m
//  collection
//
//  Created by zhangjing on 12/21/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "FeedbackController.h"

@interface FeedbackController()

@property (nonatomic, strong) IBOutlet    UITextView*     feedbackTextView;

- (IBAction)onClickBackBarButton:(id)sender;
- (IBAction)onClickSendFeedbackBarButton:(id)sender;

- (void)keyboardWillShow:(NSNotification*) notification;
- (void)keyboardWillHide:(NSNotification*) notification;

@end

@implementation FeedbackController

@synthesize feedbackTextView = _feedbackTextView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.title = @"意见反馈";
        self.hidesBottomBarWhenPushed = YES;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.feedbackTextView = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.feedbackTextView becomeFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.feedbackTextView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Keyboard event handler

- (IBAction)onClickBackBarButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onClickSendFeedbackBarButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)keyboardWillShow:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    
    NSValue* value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect frame;
    // Adjust feedback view
    frame = self.feedbackTextView.frame;
    frame.size.height -= keyboardSize.height;
    self.feedbackTextView.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect frame;
    // Adjust feedback view
    frame = self.feedbackTextView.frame;
    frame.size.height += keyboardSize.height;
    self.feedbackTextView.frame = frame;
}

@end
